const baseUrl = "http://localhost:6060";
// const baseUrl = "http://192.168.129.210:6060"
// const baseUrl = "http://192.168.137.1:6060"
// const baseUrl = "http://192.168.129.210:6060"
// const baseUrl = "http://192.168.129.210:6060"

const request = (config = {
	url: '',
	method: "get",
	data: null
}) => {
	return new Promise((resolve, reject) => {

		let token = uni.getStorageSync("token");
		const header = {};
		if (token) {
			header.Authorization = token;
		}
		if (config.url && !config.url.startsWith("http")) {
			config.url = baseUrl + config.url;
		}
		uni.request({
			...config,
			header: header,
			success: (res) => {
				if (res.data.code) {
					// 统一返回值封装
					if (res.data.code === 20000) {

						resolve(res.data.data)
					} else {
						console.log("业务异常", res.data)
						if (res.data.code === 27702) {
							reject(res.data)
						} else {

							uni.showToast({
								title: "exception: " + JSON.stringify(res.data.message)
							})
						}
					}

				} else {
					// 未统一返回值封装
					resolve(res.data)
				}
			},
			fail: (error) => {
				uni.showToast({
					title: "error: " + JSON.stringify(error)
				})
			}
		})

	})

}
export default request


// const token = uni.getStorageSync('token');
// 		const header = {};
// 		if (token) {
// 			header.Authorization = token;
// 		}
// 		uni.uploadFile({
// 			header: header,
// 			filePath: this.picture,
// 			name: 'file',
// 			url: 'http://localhost:6060/company/face-reg',
// 			success: (res) => {
// 				console.log('上传成功', res);
// 				console.log('上传完毕，开始新业务人脸认证');
// 				uni.navigateTo({
// 					url: '/pages/face-search/face-search'
// 				});
// 			}
// 		});

const uploadFile = (config = {
	url: "",
	filePath: "",
	name: "file"
}) => {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync("token");
		const header = {};
		if (token) {
			header.Authorization = token;
		}
		if (config.url && !config.url.startsWith("http")) {
			config.url = baseUrl + config.url;
		}
		uni.uploadFile({
			name: "file",
			...config,
			header: header,
			success: (res) => {
				console.log("上传成功", res)
				// console.log(typeof res.data)
				// console.log("???",res.data.code)
				const resp = JSON.parse(res.data)

				if (resp.code === 20000) {
					resolve(resp.data);
				} else {
					uni.showToast({
						title: '业务异常: ' + resp.message
					});
					reject(resp)
				}

			},
			fail: (error) => {
				console.log("上传失败", error)
				// reject()
			}
		})

	})
}

export {
	uploadFile
}